package com.gosophia.reportData.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.Page;
import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.PropertyFilter.OperatorType;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.holidayManagement.ws.HolidayManagementWebService;
import com.gosophia.reportData.ws.LaunchReportWebService;
import com.gosophia.reportData.ws.ReportDataSearchWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;

@Controller("listReportDataAjaxAction")
@Scope("prototype")
public class ListReportDataAjaxAction extends BaseAjaxAction<MissionDTO> {

	private static final long serialVersionUID = 1L;

	private String searchCond;

	@Autowired
	private ReportDataSearchWebService reportDataSearchWebService;

	private Integer pageNo;

	private Integer pageSize;

	@Autowired
	private LaunchReportWebService launchReportWebService;
	
	@Autowired
	private TaskManagementWebService taskManagementWebService;
	@Autowired
	private HolidayManagementWebService holidayManagementWebService;

	private String remind;
	private String warn;
	private String noState;

	@Override
	public String _execute() throws Exception {
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		Long userInfoId = userDetails.getUserAccountDTO().getUserInfoDTO()
				.getUserInfoId();
		if (pageNo == null) {
			pageNo = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		if (this.searchType.booleanValue() == false) {
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			page = taskManagementWebService.findAllMissionByPage(searchCond, pageSize, pageNo, orderConditions, propertyFilters);
		} else {
			if ("0".equals(remind) && "1".equals(warn) && noState == null) {
				// 提醒
				Date date = holidayManagementWebService.findTheAfterDay(
						new Date(), 2);
				PropertyFilter isNull = new PropertyFilter();
				isNull.setPropertyName("actualEndTime");
				isNull.setOperator(OperatorType.IS_NULL);
				PropertyFilter scheduleGE = new PropertyFilter(
						"scheduleEndDate", OperatorType.GE, new Date());
				PropertyFilter scheduleLE = new PropertyFilter(
						"scheduleEndDate", OperatorType.LE, date);
				isNull.and(scheduleGE.and(scheduleLE));
				// 预警
				PropertyFilter p = new PropertyFilter("actualEndTime",
						OperatorType.GT, "${scheduleEndDate}");
				p.setToCharFormat("yyyy-MM-dd");
				PropertyFilter isNotNull = new PropertyFilter();
				isNotNull.setPropertyName("actualEndTime");
				isNotNull.setOperator(OperatorType.IS_NOT_NULL);
				p.and(isNotNull);
				PropertyFilter isNull2 = new PropertyFilter();
				isNull2.setPropertyName("actualEndTime");
				isNull2.setOperator(OperatorType.IS_NULL);
				p.or(isNull2.and(new PropertyFilter("scheduleEndDate",
						OperatorType.LT, new Date())));
				// 或
				isNull.or(p);
				propertyFilters.add(isNull);
			}
			if ("0".equals(remind) && warn == null && noState == null) {
				Date date = holidayManagementWebService.findTheAfterDay(
						new Date(), 2);
				PropertyFilter isNull = new PropertyFilter();
				isNull.setPropertyName("actualEndTime");
				isNull.setOperator(OperatorType.IS_NULL);
				PropertyFilter scheduleGE = new PropertyFilter(
						"scheduleEndDate", OperatorType.GE, new Date());
				PropertyFilter scheduleLE = new PropertyFilter(
						"scheduleEndDate", OperatorType.LE, date);
				isNull.and(scheduleGE.and(scheduleLE));
				propertyFilters.add(isNull);
			}
			if ("1".equals(warn) && remind == null && noState == null) {
				PropertyFilter p = new PropertyFilter("actualEndTime",
						OperatorType.GT, "${scheduleEndDate}");
				p.setToCharFormat("yyyy-MM-dd");
				PropertyFilter isNotNull = new PropertyFilter();
				isNotNull.setPropertyName("actualEndTime");
				isNotNull.setOperator(OperatorType.IS_NOT_NULL);
				p.and(isNotNull);
				PropertyFilter isNull2 = new PropertyFilter();
				isNull2.setPropertyName("actualEndTime");
				isNull2.setOperator(OperatorType.IS_NULL);
				p.or(isNull2.and(new PropertyFilter("scheduleEndDate",
						OperatorType.LT, new Date())));
				propertyFilters.add(p);

			}
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			page = taskManagementWebService.findAllMissionByPage(pageSize, pageNo, propertyFilters, orderConditions);
		}

		if (this.page.getTotalCount() <= 0) {
			super.getRequestResult().setRsltCode("1");
		} else {
			super.getRequestResult().setRsltCode("0");
		}
		return SUCCESS;
	}

	public String getSearchCond() {
		return searchCond;
	}

	public void setSearchCond(String searchCond) {
		this.searchCond = searchCond;
	}

	public ReportDataSearchWebService getReportDataSearchWebService() {
		return reportDataSearchWebService;
	}

	public void setReportDataSearchWebService(
			ReportDataSearchWebService reportDataSearchWebService) {
		this.reportDataSearchWebService = reportDataSearchWebService;
	}

	

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getRemind() {
		return remind;
	}

	public void setRemind(String remind) {
		this.remind = remind;
	}

	public String getWarn() {
		return warn;
	}

	public void setWarn(String warn) {
		this.warn = warn;
	}

	public String getNoState() {
		return noState;
	}

	public void setNoState(String noState) {
		this.noState = noState;
	}

	@Override
	public MissionDTO getDetailData() {
		// TODO Auto-generated method stub
		return null;
	}
}
